package com.epoint.mobileoa.ui.todolist;

import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.bizlogic.mobileoa.GetFaWenDetailTask;
import com.epoint.mobileoa.datanalysis.XMLHelp;
import com.epoint.mobileoa.model.ArchiveProcess;
import com.epoint.mobileoa.model.Attach;
import com.epoint.mobileoa.model.OperationBtn;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.AlertHelp;
import com.epoint.mobileoa.tools.PropertiesHelp;
import com.epoint.mobileoa.tools.StringHelp;
import com.epoint.mobileoa.tools.ToastHelp;


/**
 * @author liyc
 * @time 2012-2-16 下午10:05:31
 * @annotation
 */
public class FaWenView extends SuperView {

	public static final int GetFaWenDetail = 1;

	private WebView wv;
	private WebView process_wv;
	private Button isShowProcessBtn;// 是否显示处理过程
	private TextView tv_clbz, tv_tjr, tv_shouwen_line1;
	private LinearLayout ll_shouwen_operation;// 按钮布局
	private LinearLayout ll_shouwen_process;

	private String dqbuzhou;// 当前步骤
	private String tjr;// 提交人

	private List<OperationBtn> btnlist;

	private List<Attach> attachList;// 附件列表

	private String messageitemguid;

	private String PCDealMsg = "该步骤请在PC端处理！";

	private int taskid = 1;
	private String archiveguid;
	private String typecode;
	private String messageItemGuid;
	private String operatorGuid;
	private String pviGuid;
	private String workItemGuid;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void initControls() {
		wv = (WebView) findViewById(R.id.wv_shouwen_content);
		process_wv = (WebView) findViewById(R.id.process_webview);
		process_wv.setVisibility(View.GONE);
		process_wv.setBackgroundColor(0);
		tv_shouwen_line1 = (TextView) findViewById(R.id.tv_shouwen_line1);
		tv_clbz = (TextView) findViewById(R.id.tv_shouwen_chulibuzhou);
		tv_tjr = (TextView) findViewById(R.id.tv_shouwen_tijiaoren);
		ll_shouwen_operation = (LinearLayout) findViewById(R.id.ll_shouwen_operation);
		isShowProcessBtn = (Button) findViewById(R.id.process_btn);
		isShowProcessBtn.setOnClickListener(this);

		ll_shouwen_process = (LinearLayout) findViewById(R.id.ll_shouwen_process);

		wv.setBackgroundColor(0);

		messageitemguid = getIntent().getStringExtra("messageitemguid");
		archiveguid = getIntent().getStringExtra("archiveguid");
		typecode = getIntent().getStringExtra("typecode");
		dqbuzhou = getIntent().getStringExtra("dqbuzhou");
		tjr = getIntent().getStringExtra("tjr");

		top_pb.setVisibility(View.VISIBLE);
		HashMap param = new HashMap();
		param.put("messageitemguid", messageitemguid);
		param.put("archiveguid", archiveguid);
		param.put("typecode", typecode);
		Task task = new GetFaWenDetailTask(this, param);
		MainService.addTask(task);

		hidenProcess();// 隐藏处理过程
		top_left_bt.setOnClickListener(this);
	}

	@Override
	public void KEYCODE_BACK() {
		finishDeal();
	}

	/**
	 * @author lilin
	 * @date 2012-8-4 下午3:51:20
	 * @annotation
	 */
	private void hidenProcess() {
		ll_shouwen_process.setVisibility(View.GONE);
		tv_shouwen_line1.setVisibility(View.GONE);
	}

	@Override
	public void onClick(View v) {
		if (v == top_right_bt) {
			AlertHelp
			.showAttachDownloadAlert(this, attachList, messageitemguid);
		} else if (v == isShowProcessBtn) {
			isShowProcess();// 是否显示处理过程
		} else if (v == top_left_bt) {
			finishDeal();
		}
	}

	/**
	 * @author lilin
	 * @date 2012-8-4 下午3:47:05
	 * @annotation 是否显示处理过程
	 */
	private void isShowProcess() {
		if (ll_shouwen_process.getVisibility() == View.VISIBLE) {
			ll_shouwen_process.setVisibility(View.GONE);
		} else {
			ll_shouwen_process.setVisibility(View.VISIBLE);
		}
		if (tv_shouwen_line1.getVisibility() == View.VISIBLE) {
			tv_shouwen_line1.setVisibility(View.GONE);
		} else {
			tv_shouwen_line1.setVisibility(View.VISIBLE);
		}
		if (process_wv.getVisibility() == View.VISIBLE) {
			process_wv.setVisibility(View.GONE);
		} else {
			process_wv.setVisibility(View.VISIBLE);
		}

	}

	@SuppressWarnings("rawtypes")
	@Override
	public void refresh(Object... param) {
		if (param[0].toString().equals(MainService.NORMAL)) {
			if (taskid == 1) {
				tv_clbz.setText(Html
						.fromHtml("<font color=red>当前步骤:</font><font color=black>"
								+ dqbuzhou + "</font>"));
				tv_tjr.setText(Html
						.fromHtml("<font color=red>提交人:</font><font color=black>"
								+ tjr + "</font>"));

				String bs = param[1].toString();
				String fawenfilenumber = StringHelp.getXMLAtt(bs,
						"fawenfilenumber");
				String fawendate = StringHelp.getXMLAtt(bs, "fawendate");
				String title = StringHelp.getXMLAtt(bs, "title");
				String keyword = StringHelp.getXMLAtt(bs, "keyword");
				// String miji = StringHelp.getXMLAtt(bs, "miji");
				// String fuzhu = StringHelp.getXMLAtt(bs, "fuzhu");
				String zhusong = StringHelp.getXMLAtt(bs, "zhusong");
				String chaosong = StringHelp.getXMLAtt(bs, "chaosong");
				String content = StringHelp.getXMLAtt(bs, "content");
				String chengbanbumen = StringHelp
						.getXMLAtt(bs, "chengbanbumen");
				String jpdguidangleimu = StringHelp.getXMLAtt(bs,
						"jpdguidangleimu");
				String baocunqixian = StringHelp.getXMLAtt(bs, "baocunqixian");
				String huanji = StringHelp.getXMLAtt(bs, "huanji");

				messageItemGuid = StringHelp.getXMLAtt(bs, "messageItemGuid");
				operatorGuid = StringHelp.getXMLAtt(bs, "operatorGuid");
				pviGuid = StringHelp.getXMLAtt(bs, "pviGuid");
				workItemGuid = StringHelp.getXMLAtt(bs, "workItemGuid");

				StringBuilder sbhtml = new StringBuilder();

				sbhtml.append(StringHelp.getSingleFaWenData(this, "编号：",
						fawenfilenumber));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "文件日期：",
						fawendate));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "标题：", title));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "主送：",
						zhusong));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "抄送：",
						chaosong));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "主题词：",
						keyword));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "内容概述：",
						content));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "紧急程度：",
						huanji));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "保管期限：",
						baocunqixian));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "存档类型：",
						jpdguidangleimu));
				sbhtml.append(StringHelp.getSingleFaWenData(this, "发文部门：",
						chengbanbumen));
				wv.loadDataWithBaseURL(null, sbhtml.toString(), "text/html",
						"utf-8", null);

				String btnStr = StringHelp.getXMLAtt(bs, "operationMessage");
				btnlist = XMLHelp
						.OperationBtnAna("<root>" + btnStr + "</root>");
				generateDealBtn();// 生成公文处理按钮

				String processStr = StringHelp.getXMLAtt(bs, "getlist");
				List<ArchiveProcess> alist = XMLHelp.ArchiveProcessAna("<root>"
						+ processStr + "</root>");
				tv_shouwen_line1.setText(Html
						.fromHtml("<font color=red>处理过程：</font>"));
				process_wv.setBackgroundColor(0);
				process_wv.getSettings().setBuiltInZoomControls(true); // 显示放大缩小
				process_wv.getSettings().setSupportZoom(true); // 可以缩放
				process_wv.getSettings().setDefaultZoom(ZoomDensity.MEDIUM);// 默认缩放模式
				process_wv.loadDataWithBaseURL(null,
						XMLHelp.pingProcessXML(alist), "text/html", "utf-8",
						null);
				// System.out.println(alist.size());
				// int tvcolor = Color.argb(55, 0, 255, 0);
				//
				// for (int i = 0; i < alist.size(); i++) {
				// TextView tv = new TextView(this);
				// tv.setSingleLine(true);
				// StringBuilder sbpro = new StringBuilder();
				// sbpro.append(StringHelp.getProcessHtml(this, "【步骤】",
				// alist.get(i).step));
				// sbpro.append(StringHelp.getProcessHtml(this, "【办理人员】",
				// alist.get(i).dealer));
				// sbpro.append(StringHelp.getProcessHtml(this, "【收到时间】",
				// alist.get(i).receiveDate));
				// sbpro.append(StringHelp.getProcessHtml(this, "【阅读时间】",
				// alist.get(i).readDate));
				// sbpro.append(StringHelp.getProcessHtml(this, "【处理时间】",
				// alist.get(i).dealDate));
				// sbpro.append(StringHelp.getProcessHtml(this, "【提交人】",
				// alist.get(i).senderName));
				//
				// sbpro.append("<font size=2 color=" + getYGColor() + ">"
				// + "【办理意见】" + "</font><font size=2 color="
				// + getBlackOrWhiteColor() + ">"
				// + alist.get(i).opinion + "</font>");
				//
				// tv.setBackgroundColor(tvcolor);
				// tv.setText(Html.fromHtml(sbpro.toString()));
				// ll_shouwen_process.addView(tv);
				//
				// if (tvcolor == Color.argb(55, 0, 255, 0)) {
				// tvcolor = Color.DKGRAY;
				// } else {
				// tvcolor = Color.argb(55, 0, 255, 0);
				// }
				// }

				String attachStr = "<root>"
						+ StringHelp.getXMLAtt(bs, "attaches") + "</root>";
				attachList = XMLHelp.AttachListAna(attachStr);
				if (attachList.size() > 0) {
					top_right_bt.setVisibility(View.VISIBLE);
					top_right_bt.setText("附件");
					top_right_bt.setOnClickListener(this);
				}
			} else if (taskid == 2) {
				HashMap maps = (HashMap) param[1];
				String type = (String) maps.get("type");
				if (type.equals("deal")) {
					String bs = (String) maps.get("bs");
					if (bs.equals("SUCCESS")) {
						ToastHelp.showToast(this, "处理成功！");
						finish();
						ShouWenView.isdeal = true;
						FaWenView.isdeal = true;
					} else {
						ToastHelp.showToast(this, "处理失败！");
					}
					m_Dialog.dismiss();
				}
			}
		}
		top_pb.setVisibility(View.INVISIBLE);
	}

	/**
	 * @author lilin
	 * @date 2012-8-4 下午3:21:09
	 * @annotation 生成公文处理按钮
	 */
	private void generateDealBtn() {
		if (PropertiesHelp.getProperty(this, "showFaWenBtn").equals("1")) {
			for (int i = 0; i < btnlist.size(); i++) {
				final int position = i;
				Button btOperation = new Button(this);

				btOperation.setText(btnlist.get(i).operationname);
				btOperation.setTag(i);

				if (getYGColor().equals("yellow")) {
					btOperation.setTextColor(Color.BLACK);
					btOperation
					.setBackgroundResource(R.drawable.btn_white_selectorl);
				} else {
					btOperation.setTextColor(Color.WHITE);
					btOperation
					.setBackgroundResource(R.drawable.btn_blue_selector);
				}
				btOperation.setTextSize(18);

				btOperation.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						String btnName = ((Button) v).getText().toString();
						String ShowOperationPage = btnlist.get(position).ShowOperationPage;
						if (btnName.indexOf("盖章") > -1) {
							ToastHelp.showToast(getApplicationContext(),
									PCDealMsg);
						} else if (ShowOperationPage.equals("1")) {
							String transactors = btnlist.get(position).transactors;
							Intent intent = new Intent(getApplicationContext(),
									ProgressTaskDealView.class);
							intent.putExtra("messageItemGuid", messageitemguid);
							intent.putExtra("pviGuid", pviGuid);
							intent.putExtra("workItemGuid", workItemGuid);
							intent.putExtra("operationGuid",
									btnlist.get(position).operationGuid);
							intent.putExtra("operatorGuid", operatorGuid);
							intent.putExtra("transitionGuid",
									btnlist.get(position).transitionGuid);
							intent.putExtra("transactors", transactors);
							intent.putExtra("guid", archiveguid);
							intent.putExtra("step", dqbuzhou);
							intent.putExtra("operateStatus",
									btnlist.get(position).operateStatus);
							intent.putExtra("tag",
									btnlist.get(position).operationname);
							String ShowNextActivit = btnlist.get(position).ShowNextActivit;
							intent.putExtra("ShowNextActivit", ShowNextActivit);
							intent.putExtra("typecode", typecode);
							startActivity(intent);
						} else {
							showDialog_wait("正在处理...");
							taskid = 2;
							HashMap<String, Object> params = new HashMap<String, Object>();
							params.put("zhusongrenguid", "");
							params.put("chaosongrenguid", "");
							params.put("messageItemGuid", messageItemGuid);
							params.put("operatorGuid", operatorGuid);
							params.put("pviGuid", pviGuid);
							params.put("workItemGuid", workItemGuid);
							params.put("operationGuid",
									btnlist.get(position).operationGuid);
							params.put("operateStatus",
									btnlist.get(position).operateStatus);
							params.put("handleOpinion", "");
							params.put("transitionGuid",
									btnlist.get(position).transitionGuid);
							params.put("typecode", typecode);
							Task task = new ShouwenDealTask(FaWenView.this,
									params);
							MainService.addTask(task);
						}

					}
				});
				ll_shouwen_operation.addView(btOperation);
			}

		}

	}

	@Override
	public int getLayoutInt() {
		return R.layout.shouwen;
	}

	@Override
	public String setTitle() {
		return "发文详情";
	}

	public static boolean isdeal = false;

	/* (non-Javadoc)
	 * @see com.epoint.mobileoa.bizlogic.SuperView#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		if (isdeal) {
			finishDeal();
		}

	}

	private void finishDeal() {
		Intent it = new Intent(this, TodolistView.class);
		startActivity(it);
		finish();
	}
	
	/* (non-Javadoc)
	 * @see com.epoint.mobileoa.bizlogic.SuperView#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		isdeal = false;
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}

}
